﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Smart.Data.BaseClasses;
using Smart.Model;

namespace Smart.Data.ExerciseCalculation
{
    public class ExerciseDL : BaseDataAccess
    {
        private ExerciseDL instance;

        public ExerciseDL(ISmartEntities context)
        {
            Context = context;
        }

        public ExerciseDL GetInstance(ISmartEntities context = null)
        {
            if (instance == null)
            {
                instance = new ExerciseDL(context);
            }
            return instance;
        }

        public List<ExerciseCategory> GetAllCategories()
        {
            return Context.ExerciseCategory.ToList<ExerciseCategory>();
        }

        public List<Exercise> GetAllExercises()
        {
            return Context.Exercise.ToList<Exercise>();
        }

        public Exercise GetExerciseById(int ExId)
        {
            return Context.Exercise.Where(e => e.ExerciseId == ExId).FirstOrDefault();
        }

        public List<Exercise> GetExercisesByCategory(int CatId)
        {
            var result = Context.Exercise.Where(e => e.CategoryId == CatId).ToList<Exercise>();

            return result;
        }

        public double CalculateBurnedCalories(int ExId, int duration)
        {
            var ex = Context.Exercise.Where(e => e.ExerciseId == ExId).FirstOrDefault();
            return (ex.CaloriesPerHour * duration) / 60;
        }

        public void RecordExercise(ExerciseLog ex)
        {
            if (ex.LogId == 0)
            {
                Context.ExerciseLog.AddObject(ex);
            }

            if (Context is SmartEntities)
            {
                // Commit all changes
                (Context as SmartEntities).SaveChanges();
            }
        }
    }
}
